<?php

/**
 * Albireo Kernel
 *
 * @copyright  Copyright (c) 2010 Albireo Solutions
 * @package    Kernel
 */

namespace KernelModule\ModulesModule;

use KernelModule\BasePresenter;
use Albireo\OAuth\AuthClient;

use \SimpleXMLIterator;

/**
 * Kernel modules management presenter.
 *
 * @author     Albireo Solutions
 * @package    Kernel
 */
class GoogleApiPresenter extends BasePresenter {

    public function actionDefault() {
        $callbackUrl = "http://localhost/AlbireoIS/document_root/kernel/modules/google-api/access";
        $scope = "https://docs.google.com/feeds/";
        
        $oAuthHandler = new AuthClient();
        $requestTokenInfo = $oAuthHandler->getRequestToken($callbackUrl, $scope);
        $this->getSession("oauth")->tokenInfo = $requestTokenInfo;
        
        $this->redirectUri($oAuthHandler->authorizationUrl($requestTokenInfo));
        throw new \Exception("The system should never reach this line");
    }
    
    public function actionAccess($oauth_token, $oauth_verifier) {
        $token_secret = $this->getSession("oauth")->tokenInfo["oauth_token_secret"];
        $oAuthHandler = new AuthClient();
        $accessTokenInfo = $oAuthHandler->getAccessToken($oauth_token, $token_secret);
        $this->getSession("oauth")->tokenInfo = $accessTokenInfo;
        
        $this->redirect("authorized");
        throw new \Exception("The system should never reach this line");
    }
    
    public function renderAuthorized() {
        $this->template->title = "Google - authorized";
        //\Nette\Debug::dump($this->getSession("oauth")->tokenInfo);
        
        $accessToken = $this->getSession("oauth")->tokenInfo["oauth_token"];
        $accessTokenSecret = $this->getSession("oauth")->tokenInfo["oauth_token_secret"];
        
        $url = "https://docs.google.com/feeds/default/private/full/-/folder";
        $oAuthHandler = new AuthClient();
        $result = $oAuthHandler->fetch($accessToken, $accessTokenSecret, $url);
        //$this->template->result = $result;
        $xml = new SimpleXMLIterator($result);
        \Nette\Debug::dump($xml);
        /* The system should ever reach this line */
        
    }

}